package com.huage_08.lesson_03;

public class Box {
    private int num;
    private boolean state = false;

    public synchronized void put(int num){
        if(state){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.num = num;
        System.out.println("送奶工已放入第"+this.num+"瓶牛奶");
        state = true;
        notifyAll();//通知消费

    }
    public synchronized void get(){
        if(!state){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者已消费第"+this.num+"瓶牛奶");
        state = false;
        notifyAll();//通知生产

    }
}
